package com.icc.test;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zaxxer.hikari.HikariDataSource;

/**
 * @ClassName Codegen
 * @Description: TODO
 * @Author ICC228979
 * @Date 2024/4/16 14:13
 * @Version 1.0
 */
public class Codegen {
    public static void main(String[] args) {
        // 配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://10.96.7.33:3306/icc_big_admin?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        // 创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        // 通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);
        // 生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        // 文件输出目录
        globalConfig.setSourceDir("D://files/java");
        // 设置根包
        globalConfig.setBasePackage("com.icc.test");
        // 设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(17);
        // 设置表前缀和只生成哪些表
        // globalConfig.setTablePrefix("tb_");
        globalConfig.setGenerateTable("db_manage");

        // Entity 生成配置 EntityConfig
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.getEntityConfig()
                .setWithLombok(true)
                .setWithSwagger(true)
                .setSwaggerVersion(EntityConfig.SwaggerVersion.DOC)
                // .setClassPrefix("My")
                .setClassSuffix("Entity")
        // .setSuperClass(BaseEntity.class)
        ;

        // Mapper 生成配置 MapperConfig
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.getMapperConfig()
                // .setClassPrefix("My")
                .setClassSuffix("Mapper")
                .setSuperClass(BaseMapper.class);

        // MapperXml 生成配置 MapperXmlConfig
        globalConfig.setMapperXmlGenerateEnable(true);
        globalConfig.getMapperXmlConfig()
                // .setFilePrefix("My")
                .setFileSuffix("Mapper");


        //  Service 生成配置 ServiceConfig
        globalConfig.setServiceGenerateEnable(true);
        globalConfig.getServiceConfig()
                // .setClassPrefix("My")
                .setClassSuffix("Service")
                .setSuperClass(IService.class);

        // ServiceImpl 生成配置 ServiceImplConfig
        globalConfig.setServiceImplGenerateEnable(true);
        globalConfig.getServiceImplConfig()
                // .setClassPrefix("My")
                .setClassSuffix("ServiceImpl")
                .setSuperClass(ServiceImpl.class);


        // Controller 生成配置 ControllerConfig
        globalConfig.setControllerGenerateEnable(true);
        globalConfig.getControllerConfig()
                // .setClassPrefix("My")
                .setClassSuffix("Controller")
        // .setSuperClass(BaseController.class)
        ;


        return globalConfig;
    }
}
